package com.chenjun.web.controller.sys;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chenjun.common.base.ResultDto;
import com.chenjun.common.bean.Pagination;
import com.chenjun.common.model.DataGridModel;
import com.chenjun.common.model.DataGridSortModel;
import com.chenjun.core.dao.generator.entity.SysRoleResource;
import com.chenjun.core.service.sys.SysRoleResourceService;
import com.chenjun.core.vo.sys.RoleResourceVo;
import com.chenjun.web.controller.BaseController;

/**
 * 
 * @ClassName: RoleResourceService 
 * @author Sean Cui
 * @date 2016-06-14 下午17:50:00 
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @version V1.0.0.0
 * @comment 该文件通过代码生成器生成，如果修改次文件后，重新生成时将覆盖修改内容，请修改生成模板或做好备份
 */

@SuppressWarnings("serial")
@Controller
@RequestMapping("roleResource")
public class SysRoleResourceController extends BaseController {

	@Autowired
	private SysRoleResourceService sysRoleResourceService;
	
	/**
	 * 页面展示
	 * @return
	 * ModelAndView
	 */
	@RequestMapping("/list.html")
	public ModelAndView index() {
		ModelAndView mv = new ModelAndView("roleResource/list");
		//补充业务逻辑
		return mv;
	}
	
	/**
	 * 分页搜索
	 * @param datagrid
	 * @return
	 * ResultDto
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	@RequestMapping(value = "/search")
	@ResponseBody
	public ResultDto list(DataGridModel datagrid) {
		ResultDto rtDto = new ResultDto();
		Pagination data = sysRoleResourceService.doPagination(datagrid);
		rtDto.setData(data);
		return rtDto;
	}

	/**
	 * 编辑页面元素
	 * @return
	 * ResultDto
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public ResultDto edit(@RequestBody @Valid RoleResourceVo roleResourceVo,Errors errors)  throws IllegalAccessException, InvocationTargetException{
		ResultDto rtDto = new ResultDto();
		
		//验证
		if (!hanleErrors(rtDto, errors)) { return rtDto; }
		
		int i = sysRoleResourceService.batchCreateOrUpdate(roleResourceVo);
		if(i<=0){
			rtDto.resultError();
			rtDto.setMessage("编辑系统异常");
		}
		return rtDto;
	}

	/**
	 * 删除指定的记录
	 * TODO
	 * @param id
	 * @return
	 * ResultDto
	 */
	@RequestMapping("/delete/{id}")
	@ResponseBody
	public ResultDto delete(@PathVariable String id) {
		ResultDto rtDto = new ResultDto();
		SysRoleResource sysRoleResource = sysRoleResourceService.get(id);
		int result = 0;
		if (sysRoleResource != null) {
			result = sysRoleResourceService.delete(id);
		}
		if (result <= 0) {
			rtDto.resultError();
			rtDto.setMessage("要删除的记录不存在!");
		}
		return rtDto;
	}

	/**
	 * 根据参数获取信息
	 * TODO
	 * @param id
	 * @return
	 * ResultDto
	 */
	@RequestMapping("/get/{id}")
	@ResponseBody
	public ResultDto get(@PathVariable String id) {
		ResultDto rtDto = new ResultDto();
		rtDto.setData(sysRoleResourceService.get(id));
		return rtDto;
	}
	
	@RequestMapping(value = "/doSearch/{roleId}")
	@ResponseBody
	@RequiresPermissions("role:base")
	public ResultDto searchById(@PathVariable String roleId) {
		ResultDto rtDto = new ResultDto();
		DataGridModel dgm = new DataGridModel();
		dgm.setStart(0);
		dgm.setRows(0);
		dgm.getParams().put("roleId",roleId);
		dgm.getParams().put("nParentId","0");
		dgm.getSortBy().add(new DataGridSortModel("sort_num", DataGridSortModel.ASC));
		List<SysRoleResource> sysRoleResources = sysRoleResourceService.doListByCondition(dgm);
		rtDto.setData(sysRoleResources);
		return rtDto;
	}
	
	/*@RequestMapping(value = "/addroleresource")
	@ResponseBody
	@RequiresPermissions("role:base")
	public ResultDto addRoleResource(@RequestBody RoleResourceSaveBean dto) {
		ResultDto rtDto = new ResultDto();
		boolean validatePass = true;
		if (StringUtils.isBlank(dto.getRoleId())) {
			validatePass = false;
		}
		if (validatePass) {
			String roleId = dto.getRoleId();
			roleResourceService.deleteByRoleId(roleId);
			List<String> resources = dto.getResources();
			if (resources != null && resources.size() > 0) {
				Date date = new Date();
				for (String resourceId : resources) {
					RoleResource roleResource = new RoleResource();
					roleResource.setResourceId(resourceId);
					roleResource.setRoleId(roleId);
					roleResource.setCreatedTime(date);
					roleResourceService.create(roleResource);
				}
			}
			
			authService.reCreateFilterChains();
			
		} else {
			rtDto.resultWaring();
			rtDto.setMessage("数据校验失败");
		}
		return rtDto;
	}*/

}
